import { versionInfo } from "@api";
import Configs from "@config";
import { storeUpdateInfo } from "@utils";

const version = Configs.version;

//启动一个定时器
chrome.alarms.create("check-version", {
  when: Date.now(),
  periodInMinutes: 1,
});

chrome.alarms.onAlarm.addListener(function () {
  versionInfo()
    .then(({ data: result }) => {
      console.log("更新信息获取成功,", result);
      const { data } = result;
      const updateInfo = {
        update: false,
        data: data,
      };

      let localVersion = +`${version.code}`;
      let updateVersion = +`${data.version.cur}`;

      //标记是否要更新
      updateInfo.update =
        localVersion !== updateVersion && updateVersion > localVersion
          ? true
          : false;
      console.log("saved update info is:", updateInfo);
      storeUpdateInfo(updateInfo);
    })
    .catch((err) => {
      console.log("获取版本信息失败,错误信息:", err);
    });
});
